import math

from pyecharts.charts import Kline, Line, Grid
from pyecharts import options as opts


class KlinePanel:

    def __init__(self, context, file, title='Aquank看板'):
        self.ma20 = None
        self.inComes = None
        self.xLines = None
        self.context = context
        self.file = file
        self.kline = Kline()
        self.title = title
        self.marks = []

    def addMark(self, mark):

        self.xLines = self.context.data['day'].values.tolist()
        index = mark['index']
        markPoint = opts.MarkPointItem(
            # 标注名称
            name=mark['name'],
            # 标注的坐标。坐标格式视系列的坐标系而定，可以是直角坐标系上的 x, y，
            # 也可以是极坐标系上的 radius, angle。
            coord=[self.xLines[index], mark['value']],
            # 标注值，可以不设。
            value=mark['name'],
            symbol_size=30,
            itemstyle_opts=opts.ItemStyleOpts(color=mark['color'], opacity=1),
        )
        self.marks.append(markPoint)
    def draw(self):
        # 创建k线对象
        data = self.context.data[['open', 'close', 'low', 'high']].values.tolist()

        self.kline.add_xaxis(self.xLines)
        self.kline.add_yaxis(series_name="K线图", y_axis=data,
                             markpoint_opts=opts.MarkPointOpts(
                data=self.marks
            ))
        self.kline.set_global_opts(
            xaxis_opts=opts.AxisOpts(

                name='日期',
                is_scale=True,
            ),
            yaxis_opts=opts.AxisOpts(
                name='',
                is_scale=True,
                splitarea_opts=opts.SplitAreaOpts(
                    is_show=True,
                    areastyle_opts=opts.AreaStyleOpts(opacity=0.7)
                ),
            ),
            datazoom_opts=[
                opts.DataZoomOpts(
                    is_show=False,
                    type_="inside",
                    xaxis_index=[0, 1],
                    range_start=80,
                    range_end=100,
                ),
                # xaxis_index=[0, 0]设置第一幅图为内部缩放
                opts.DataZoomOpts(
                    is_show=True,
                    type_="slider",
                    xaxis_index=[0, 1],
                    pos_top="95%",
                    range_start=80,
                    range_end=100,
                ),
                # xaxis_index=[0, 1]连接第二幅图的axis
            ],
            title_opts=opts.TitleOpts(title=self.title, subtitle=self.title),
            tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'),
                                   )
        fast_MA, slow_MA = self.context.MA5, self.context.MA10

        atrLine = Line()
        atrLine.add_xaxis(self.xLines)
        atr, atrlow = self.context.ATR, self.context.ATRLOW

        atrLine.add_yaxis(
            series_name="ATR_HIGH" + str(20),
            y_axis=atr,
            is_smooth=True,
            linestyle_opts=opts.LineStyleOpts(opacity=0.9, width=2),
            label_opts=opts.LabelOpts(is_show=False),  # 不显示具体数值
            z=3,  # ma在最顶层
        )

        atrLine.add_yaxis(
            series_name="MA20",
            y_axis=self.context.MA20,
            is_smooth=True,
            linestyle_opts=opts.LineStyleOpts(opacity=0.9, width=2),
            label_opts=opts.LabelOpts(is_show=False),  # 不显示具体数值
            z=3,  # ma在最顶层
        )

        atrLine.add_yaxis(
            series_name="ATR_LOW" + str(20),
            y_axis=atrlow,
            is_smooth=True,
            linestyle_opts=opts.LineStyleOpts(opacity=0.9, width=2),
            label_opts=opts.LabelOpts(is_show=False),
            z=4,
        )
        inComeLine = Line()
        inComeLine.add_xaxis(self.xLines)
        inComeLine.add_yaxis(
            series_name="",
            y_axis=self.inComes,
            is_smooth=True,
            linestyle_opts=opts.LineStyleOpts(opacity=0.9, width=3, color="#ff0000"),
            label_opts=opts.LabelOpts(is_show=False),  # 不显示具体数值
            z=5,

        )
        inComeLine.set_global_opts(
            yaxis_opts=opts.AxisOpts(
                name=f' 收益： {math.ceil(self.inComes[-1])}%',
                is_scale=True,
                splitarea_opts=opts.SplitAreaOpts(
                    is_show=True,
                    areastyle_opts=opts.AreaStyleOpts(opacity=0.4, )
                ),

            ),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            title_opts=opts.TitleOpts(title=f'收益: {math.ceil(self.inComes[len(self.inComes)-1])}%', pos_left="3%", pos_right="3%", pos_top="40%")
        )

        self.kline.overlap(atrLine)

        gridChart = Grid(init_opts=opts.InitOpts(width="1200px", height="800px", bg_color="#aaaaaa"))
        gridChart.add(self.kline, grid_opts=opts.GridOpts(pos_left="3%", pos_right="3%", height="30%"))
        gridChart.add(
            inComeLine,
            grid_opts=opts.GridOpts(pos_left="3%", pos_right="3%", pos_top="40%", height="20%"),
        )

        gridChart.render(self.file)

    def setIncomes(self, incomes):
        self.inComes = incomes
    def setMa20(self, ma20):
        self.ma20 = ma20
